package cn.e_eap.esite.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import cn.e_eap.esite.bean.Module;
import cn.e_eap.esite.manager.ModuleManager;
import cn.e_eap.esite.web.form.ModuleForm;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @author Asia
 * @since 2012-11-1
 */
public class ModuleAction extends BaseAction implements ModelDriven<ModuleForm> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3889039746517329851L;

	private List<Module> datas;

	private String ids;
	private Module module;
	private ModuleForm form;
	private ModuleManager moduleManager;

	public String delete() {
		moduleManager.delete(ids);
		return super.delete();
	}

	public String doAdd() {
		module = new Module();
		BeanUtils.copyProperties(form, module);
		moduleManager.add(module, form.getParentId());
		return super.doAdd();
	}

	public String doUpdate() {
		module = moduleManager.findById(form.getId());
		BeanUtils.copyProperties(form, module);
		if ((module.getParent() == null || module.getParent().getId() == 0)
				&& form.getParentId() > 0)
			module.setParent(moduleManager.findById(form.getParentId()));
		moduleManager.update(module);
		return super.doUpdate();
	}

	public String execute() {
		datas = moduleManager.find(form, this);
		module = moduleManager.findById(form.getParentId());
		return super.execute();
	}

	public List<Module> getDatas() {
		return datas;
	}

	public String getIds() {
		return ids;
	}

	@Override
	public ModuleForm getModel() {
		if (form == null)
			form = new ModuleForm();
		return form;
	}

	public Module getModule() {
		return module;
	}

	public ModuleManager getModuleManager() {
		return moduleManager;
	}

	public String input() {
		if (form.getId() > 0) {
			module = moduleManager.findById(form.getId());
		}
		datas = moduleManager.find(new ModuleForm(), null);
		return super.input();
	}

	public void setDatas(List<Module> datas) {
		this.datas = datas;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	@Resource
	public void setModuleManager(ModuleManager moduleManager) {
		this.moduleManager = moduleManager;
	}

}
